﻿package  
{
	import adobe.utils.CustomActions;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	import flash.filters.GlowFilter;
	import flash.filters.BitmapFilterQuality;
	import com.greensock.TweenMax;
	import com.greensock.easing.*;
	/**
	 * ...
	 * @author Tuan
	 */
	public class mcGallery extends MovieClip
	{
		var loader:URLLoader;
		var myRequest:URLRequest;
		var xml:XML;
		var imgRequest:URLRequest;
		var images:Array;
		var bigImages:Array;
		var currentIndex:int = 0;
		var checkLoadFinish:int = 0;
		var checkLoadBigImagesFinish:int = 0;
		var direction:int = 1;
		var thumbnailBar1Over:Boolean = false;
		
		public function mcGallery() 
		{
			images = new Array();
			bigImages = new Array();
			myRequest = new URLRequest("Gallery/XML/Gallery.xml");
			loader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, onLoadComplete);
			loader.load(myRequest);
		}
		
		//load duong dan anh tu file xml o day
		public function onLoadComplete(e:Event):void
		{
			if (e.currentTarget == loader)
			{
				xml = new XML(loader.data);
				for (var i:int = 0; i < xml.img.length(); i++)
				{
					imgRequest = new URLRequest(xml.img[i].@src);
					images[i] = new Loader();
					bigImages[i] = new Loader();
					Loader(images[i]).contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoadedComplete);
					Loader(images[i]).load(imgRequest);
					//currentIndex = i;
					
					Loader(bigImages[i]).contentLoaderInfo.addEventListener(Event.COMPLETE, onBigImageLoadedComplete);
					Loader(bigImages[i]).load(imgRequest);
				}
				this.thumbnailBar1.length = (xml.img.length() * 40) + 10;
			}
		}
		
		public function onImageLoadedComplete(e:Event):void
		{
			checkLoadFinish++;
			showImages();
		}
		
		public function onBigImageLoadedComplete(e:Event):void
		{
			checkLoadBigImagesFinish++;
			showBigImages();
		}
		
		public function locateImage(child:Loader, background:MovieClip, bound:Number):void
		{
			var a:Number = child.width / child.height;
			if (a > 1)
			{
				child.width = background.width - (bound*2);
				child.height = child.width / a;
			}
			else
			{
				child.height = background.height - (bound*2);
				child.width = child.height * a;
			}
			child.x = (background.width - child.width) / 2;
			child.y = (background.height - child.height) / 2;
			background.addChild(child);
		}
		
		public function showImages():void
		{
			if (checkLoadFinish == xml.img.length())
			{
				for (var i:int = 0; i < xml.img.length(); i++)
				{
					var thumbnail:mcThumbnail = new mcThumbnail();
					this.thumbnailBar1.addChild(thumbnail);
					thumbnail.x = 10;
					thumbnail.y = 10 + (i * 50);
					locateImage(images[i], thumbnail, 4);
					thumbnail.addEventListener(MouseEvent.CLICK, imageClicked);
				}
				this.thumbnailBar1.addEventListener(MouseEvent.MOUSE_OVER, thumbnailBar1mOver);
				this.addEventListener(Event.ENTER_FRAME, onFrame);
				focusSmallImage(images[0]);
			}
		}
		
		public function showBigImages():void
		{
			if (checkLoadBigImagesFinish == xml.img.length())
			{
				locateImage(bigImages[0], this.bgImage1, 0);
			}
		}
		
		public function thumbnailBar1mOver(e:MouseEvent):void
		{
			var negativeY:Number = 400 - this.thumbnailBar1.height;
			if (mouseY / stage.stageHeight > 0.5)
			{
				direction = 1;
			}
			else
			{
				direction = -1;
			}
			if ((direction == 1 && this.thumbnailBar1.y < 0) ||
				(direction== -1 && this.thumbnailBar1.y > negativeY))
			{
				thumbnailBar1Over = true;
			}
		}
		
		public function onFrame(e:Event):void
		{
			var negativeY:Number = 400 - this.thumbnailBar1.height;
			if (thumbnailBar1Over)
			{
				this.thumbnailBar1.y += (direction * 2);
			}
			if (this.thumbnailBar1.y < negativeY || this.thumbnailBar1.y > 0)
			{
				thumbnailBar1Over = false;
			}
		}
		
		//small images clicked event here
		public function imageClicked(e:MouseEvent):void
		{			
			var index:int = 0;
			thumbnailBar1Over = false;
			
			images[currentIndex].filters = null;
			
			TweenMax.to(bigImages[currentIndex], 0.7, { alpha:0, onComplete:bigImagesChanged, ease:Quart.easeInOut } );
			this.bgImage1.removeChild(bigImages[currentIndex]);
			
			//set new value for currentIndex
			currentIndex = (e.currentTarget.y - 10) / 50;
			
			//replace images
			focusSmallImage(images[currentIndex]);
		}
		
		public function focusSmallImage(loader:Loader):void
		{
			var glow:GlowFilter = new GlowFilter;
			glow.color = 0xFFFF66;
			glow.quality = BitmapFilterQuality.HIGH;
			loader.filters = [glow];
		}
		
		public function bigImagesChanged():void
		{
			locateImage(bigImages[currentIndex], this.bgImage1, 0);
			bigImages[currentIndex].alpha = 0;
			TweenMax.to(bigImages[currentIndex], 0.7, { alpha:1, ease:Quart.easeInOut } );
		}
	}

}